
Enemy = class("Enemy")

function Enemy:ctor(gameObject)
     --self对应的对象的实例本身的表
    self.gameObject = gameObject
    self.transform = self.gameObject.transform

    self.fireTime = 0
    self.enemyspeed = 3
    self.enemyfirespeed = 5

    local luaTrigger = self.gameObject:AddComponent(typeof(LuaRigidTrigger))
    luaTrigger:AddHandler(self.OnTriggerEnter2D,self)

    UpdateBeat:Add(self.Update,self)
end

function Enemy:Update ()
    self.transform:Translate(Vector3.down * self.enemyspeed * Time.deltaTime)

    self.CheckFire(self)
end

function Enemy:CheckFire()
    if ( Time.time - self.fireTime > 1 ) then
        self.fireTime = Time.time
        local bullet = newObject(ThunderManager.prefabs[3]).transform
        self.firepos = self.gameObject.transform:Find("FirePoint")
        bullet.transform.position = self.firepos.position
        bullet.transform.rotation = self.firepos.rotation
        bullet.gameObject:GetComponent(typeof(Rigidbody2D)).velocity = -self.firepos.up * self.enemyfirespeed

        --销毁 敌机子弹（1秒）
        GameObject.Destroy(bullet.gameObject,3)
    end
end

function Enemy:OnTriggerEnter2D(collider)
    if (collider.gameObject:CompareTag("Player")) then
        log("自身和敌机发生碰撞")  
    end
    if (collider.gameObject:CompareTag("bullet")) then
        log("子弹打中敌机")

        UpdateBeat:Remove(self.Update,self)

        --加分
        Player.Score()

        --销毁 自己的子弹
        destroy(collider.gameObject)
        --销毁 敌机
        destroy(self.gameObject)
    end
end
